home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / Firefox Setup 3.0.6.exe / nonlocalized / components / nsSessionStartup.js < prev    next >
Encoding:
Text File  |  2009-01-19  |  11.2 KB  |  320 lines

  1. /* 
  2. //@line 38 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\sessionstore\src\nsSessionStartup.js"
  3. */
  4.  
  5. /**
  6. //@line 64 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\sessionstore\src\nsSessionStartup.js"
  7. */
  8.  
  9. /* :::::::: Constants and Helpers ::::::::::::::: */
  10.  
  11. const Cc = Components.classes;
  12. const Ci = Components.interfaces;
  13. const Cr = Components.results;
  14. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  15.  
  16. const STATE_RUNNING_STR = "running";
  17.  
  18. function debug(aMsg) {
  19.   aMsg = ("SessionStartup: " + aMsg).replace(/\S{80}/g, "$&\n");
  20.   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
  21.                                      .logStringMessage(aMsg);
  22. }
  23.  
  24. /* :::::::: The Service ::::::::::::::: */
  25.  
  26. function SessionStartup() {
  27. }
  28.  
  29. SessionStartup.prototype = {
  30.  
  31.   // the state to restore at startup
  32.   _iniString: null,
  33.   _sessionType: Ci.nsISessionStartup.NO_SESSION,
  34.  
  35. /* ........ Global Event Handlers .............. */
  36.  
  37.   /**
  38.    * Initialize the component
  39.    */
  40.   init: function sss_init() {
  41.     this._prefBranch = Cc["@mozilla.org/preferences-service;1"].
  42.                        getService(Ci.nsIPrefService).getBranch("browser.");
  43.  
  44.     // if the service is disabled, do not init 
  45.     if (!this._prefBranch.getBoolPref("sessionstore.enabled"))
  46.       return;
  47.  
  48.     // get file references
  49.     var dirService = Cc["@mozilla.org/file/directory_service;1"].
  50.                      getService(Ci.nsIProperties);
  51.     this._sessionFile = dirService.get("ProfD", Ci.nsILocalFile);
  52.     this._sessionFile.append("sessionstore.js");
  53.     
  54.     // only read the session file if config allows possibility of restoring
  55.     var resumeFromCrash = this._prefBranch.getBoolPref("sessionstore.resume_from_crash");
  56.     if ((resumeFromCrash || this._doResumeSession()) && this._sessionFile.exists()) {
  57.       // get string containing session state
  58.       this._iniString = this._readFile(this._sessionFile);
  59.       if (this._iniString) {
  60.         try {
  61.           // parse the session state into JS objects
  62.           var s = new Components.utils.Sandbox("about:blank");
  63.           var initialState = Components.utils.evalInSandbox(this._iniString, s);
  64.  
  65.           // set bool detecting crash
  66.           this._lastSessionCrashed =
  67.             initialState.session && initialState.session.state &&
  68.             initialState.session.state == STATE_RUNNING_STR;
  69.         // invalid .INI file - nothing can be restored
  70.         }
  71.         catch (ex) { debug("The session file is invalid: " + ex); } 
  72.       }
  73.     }
  74.  
  75.     // prompt and check prefs
  76.     if (this._iniString) {
  77.       if (this._lastSessionCrashed && this._doRecoverSession())
  78.         this._sessionType = Ci.nsISessionStartup.RECOVER_SESSION;
  79.       else if (!this._lastSessionCrashed && this._doResumeSession())
  80.         this._sessionType = Ci.nsISessionStartup.RESUME_SESSION;
  81.       else
  82.         this._iniString = null; // reset the state string
  83.     }
  84.  
  85.     if (this._prefBranch.getBoolPref("sessionstore.resume_session_once")) {
  86.       this._prefBranch.setBoolPref("sessionstore.resume_session_once", false);
  87.     }
  88.     
  89.     if (this._sessionType != Ci.nsISessionStartup.NO_SESSION) {
  90.       // wait for the first browser window to open
  91.       var observerService = Cc["@mozilla.org/observer-service;1"].
  92.                             getService(Ci.nsIObserverService);
  93.       observerService.addObserver(this, "domwindowopened", true);
  94.     }
  95.   },
  96.  
  97.   /**
  98.    * Handle notifications
  99.    */
  100.   observe: function sss_observe(aSubject, aTopic, aData) {
  101.     var observerService = Cc["@mozilla.org/observer-service;1"].
  102.                           getService(Ci.nsIObserverService);
  103.  
  104.     switch (aTopic) {
  105.     case "app-startup": 
  106.       observerService.addObserver(this, "final-ui-startup", true);
  107.       break;
  108.     case "final-ui-startup": 
  109.       observerService.removeObserver(this, "final-ui-startup");
  110.       this.init();
  111.       break;
  112.     case "domwindowopened":
  113.       var window = aSubject;
  114.       var self = this;
  115.       window.addEventListener("load", function() {
  116.         self._onWindowOpened(window);
  117.         window.removeEventListener("load", arguments.callee, false);
  118.       }, false);
  119.       break;
  120.     }
  121.   },
  122.  
  123.   /**
  124.    * Removes the default arguments from the first browser window
  125.    * (and removes the "domwindowopened" observer afterwards).
  126.    */
  127.   _onWindowOpened: function sss_onWindowOpened(aWindow) {
  128.     var wType = aWindow.document.documentElement.getAttribute("windowtype");
  129.     if (wType != "navigator:browser")
  130.       return;
  131.     
  132.     /**
  133.      * Note: this relies on the fact that nsBrowserContentHandler will return
  134.      * a different value the first time its getter is called after an update,
  135.      * due to its needHomePageOverride() logic. We don't want to remove the
  136.      * default arguments in the update case, since they include the "What's
  137.      * New" page.
  138.      *
  139.      * Since we're garanteed to be at least the second caller of defaultArgs
  140.      * (nsBrowserContentHandler calls it to determine which arguments to pass
  141.      * at startup), we know that if the window's arguments don't match the
  142.      * current defaultArguments, we're either in the update case, or we're
  143.      * launching a non-default browser window, so we shouldn't remove the
  144.      * window's arguments.
  145.      */
  146.     var defaultArgs = Cc["@mozilla.org/browser/clh;1"].
  147.                       getService(Ci.nsIBrowserHandler).defaultArgs;
  148.     if (aWindow.arguments && aWindow.arguments[0] &&
  149.         aWindow.arguments[0] == defaultArgs)
  150.       aWindow.arguments[0] = null;
  151.     
  152.     var observerService = Cc["@mozilla.org/observer-service;1"].
  153.                           getService(Ci.nsIObserverService);
  154.     observerService.removeObserver(this, "domwindowopened");
  155.   },
  156.  
  157. /* ........ Public API ................*/
  158.  
  159.   /**
  160.    * Get the session state as a string
  161.    */
  162.   get state() {
  163.     return this._iniString;
  164.   },
  165.  
  166.   /**
  167.    * Determine whether there is a pending session restore.
  168.    * @returns bool
  169.    */
  170.   doRestore: function sss_doRestore() {
  171.     return this._sessionType != Ci.nsISessionStartup.NO_SESSION;
  172.   },
  173.  
  174.   /**
  175.    * Get the type of pending session store, if any.
  176.    */
  177.   get sessionType() {
  178.     return this._sessionType;
  179.   },
  180.  
  181. /* ........ Auxiliary Functions .............. */
  182.  
  183.   /**
  184.    * Whether or not to resume session, if not recovering from a crash.
  185.    * @returns bool
  186.    */
  187.   _doResumeSession: function sss_doResumeSession() {
  188.     return this._prefBranch.getIntPref("startup.page") == 3 || 
  189.       this._prefBranch.getBoolPref("sessionstore.resume_session_once");
  190.   },
  191.  
  192.   /**
  193.    * prompt user whether or not to restore the previous session,
  194.    * if the browser crashed
  195.    * @returns bool
  196.    */
  197.   _doRecoverSession: function sss_doRecoverSession() {
  198.     // do not prompt or resume, post-crash
  199.     if (!this._prefBranch.getBoolPref("sessionstore.resume_from_crash"))
  200.       return false;
  201.  
  202.     // if the prompt fails, recover anyway
  203.     var recover = true;
  204.  
  205.     // allow extensions to hook in a more elaborate restore prompt
  206.     // XXXzeniko drop this when we're using our own dialog instead of a standard prompt
  207.     var dialogURI = null;
  208.     try {
  209.       dialogURI = this._prefBranch.getCharPref("sessionstore.restore_prompt_uri");
  210.     }
  211.     catch (ex) { }
  212.     
  213.     try {
  214.       if (dialogURI) { // extension provided dialog 
  215.         var params = Cc["@mozilla.org/embedcomp/dialogparam;1"].
  216.                      createInstance(Ci.nsIDialogParamBlock);
  217.         // default to recovering
  218.         params.SetInt(0, 0);
  219.         Cc["@mozilla.org/embedcomp/window-watcher;1"].
  220.         getService(Ci.nsIWindowWatcher).
  221.         openWindow(null, dialogURI, "_blank", 
  222.                    "chrome,modal,centerscreen,titlebar", params);
  223.         recover = params.GetInt(0) == 0;
  224.       }
  225.       else { // basic prompt with no options
  226.         // get app name from branding properties
  227.         var brandStringBundle = this._getStringBundle("chrome://branding/locale/brand.properties");
  228.         var brandShortName = brandStringBundle.GetStringFromName("brandShortName");
  229.  
  230.         // create prompt strings
  231.         var ssStringBundle = this._getStringBundle("chrome://browser/locale/sessionstore.properties");
  232.         var restoreTitle = ssStringBundle.formatStringFromName("restoredTitle", [brandShortName], 1);
  233.         var restoreText = ssStringBundle.formatStringFromName("restoredMsg", [brandShortName], 1);
  234.         var okTitle = ssStringBundle.GetStringFromName("okTitle");
  235.         var cancelTitle = ssStringBundle.GetStringFromName("cancelTitle");
  236.  
  237.         var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].
  238.                             getService(Ci.nsIPromptService);
  239.  
  240.         // set the buttons that will appear on the dialog
  241.         var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
  242.                     promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
  243.                     promptService.BUTTON_POS_0_DEFAULT;
  244.         
  245.         var buttonChoice = promptService.confirmEx(null, restoreTitle, restoreText, 
  246.                                           flags, okTitle, cancelTitle, null, 
  247.                                           null, {});
  248.         recover = (buttonChoice == 0);
  249.       }
  250.     }
  251.     catch (ex) { dump(ex + "\n"); } // if the prompt fails, recover anyway
  252.     return recover;
  253.   },
  254.  
  255.   /**
  256.    * Convenience method to get localized string bundles
  257.    * @param aURI
  258.    * @returns nsIStringBundle
  259.    */
  260.   _getStringBundle: function sss_getStringBundle(aURI) {
  261.     var bundleService = Cc["@mozilla.org/intl/stringbundle;1"].
  262.                         getService(Ci.nsIStringBundleService);
  263.     var appLocale = Cc["@mozilla.org/intl/nslocaleservice;1"].
  264.                     getService(Ci.nsILocaleService).getApplicationLocale();
  265.     return bundleService.createBundle(aURI, appLocale);
  266.   },
  267.  
  268. /* ........ Storage API .............. */
  269.  
  270.   /**
  271.    * reads a file into a string
  272.    * @param aFile
  273.    *        nsIFile
  274.    * @returns string
  275.    */
  276.   _readFile: function sss_readFile(aFile) {
  277.     try {
  278.       var stream = Cc["@mozilla.org/network/file-input-stream;1"].
  279.                    createInstance(Ci.nsIFileInputStream);
  280.       stream.init(aFile, 0x01, 0, 0);
  281.       var cvstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
  282.                      createInstance(Ci.nsIConverterInputStream);
  283.       cvstream.init(stream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  284.       
  285.       var content = "";
  286.       var data = {};
  287.       while (cvstream.readString(4096, data)) {
  288.         content += data.value;
  289.       }
  290.       cvstream.close();
  291.       
  292.       return content.replace(/\r\n?/g, "\n");
  293.     }
  294.     catch (ex) { } // inexisting file?
  295.     
  296.     return null;
  297.   },
  298.  
  299.   /* ........ QueryInterface .............. */
  300.   QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver,
  301.                                           Ci.nsISupportsWeakReference,
  302.                                           Ci.nsISessionStartup]),
  303.   classDescription: "Browser Session Startup Service",
  304.   classID:          Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}"),
  305.   contractID:       "@mozilla.org/browser/sessionstartup;1",
  306.  
  307.   // get this contractID registered for certain categories via XPCOMUtils
  308.   _xpcom_categories: [
  309.     // make ourselves a startup observer
  310.     { category: "app-startup", service: true }
  311.   ]
  312.  
  313. };
  314.  
  315. //module initialization
  316. function NSGetModule(aCompMgr, aFileSpec) {
  317.   return XPCOMUtils.generateModule([SessionStartup]);
  318. }
  319.  
  320.